package ru.spbu.math.android.cbrf.view;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.R.drawable;
import ru.spbu.math.android.cbrf.R.id;
import ru.spbu.math.android.cbrf.R.layout;
import ru.spbu.math.android.cbrf.model.RateType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: yan lobkarev
 * mail: yan.lobkarev@gmail.com
 * Date: 19.03.2010
 * Time: 19:31:15
 */
public class RateTypesListAdapter implements SpinnerAdapter {

    /**
	 * @uml.property  name="lastSelectedItem"
	 * @uml.associationEnd  
	 */
    static RateType lastSelectedItem;
    Context context;

    List<RateType> actives;

    public static void deactivate(){
        lastSelectedItem = null;
    }

    public RateTypesListAdapter(Context context, List<RateType> actives) {

        this.actives = actives;
        this.context = context;
    }

    public View getDropDownView(int i, View view, ViewGroup viewGroup) {

        return makeViewByRateType(actives.get(i));

    }

    public int getViewTypeCount() {
        return actives.size();
    }

    public boolean isEmpty() {
        return actives.isEmpty();
    }

    public int getItemViewType(int i) {
        return 0;
    }

    public boolean hasStableIds() {
        return false;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        return getDropDownView(i, view, viewGroup);
    }

    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }

    public int getCount() {
        return actives.size();
    }

    public Object getItem(int i) {
        lastSelectedItem = actives.get(i);
        return lastSelectedItem;
    }

    public long getItemId(int i) {
        return i;
    }

    public View makeViewByRateType(RateType rt) {
        View res = LayoutInflater.from(context).inflate(R.layout.actives_list_item, null);

            ImageView imView = ((ImageView) res.findViewById(R.id.item_icon));
            if (lastSelectedItem != null && lastSelectedItem.getSourceCode().equals(rt.getSourceCode())) {
                imView.setImageResource(R.drawable.list_item_icon_active);
            } else {
                imView.setImageResource(R.drawable.list_item_icon_inactive);
            }

            ((TextView) res.findViewById(R.id.toptext)).setText(rt.getSourceCode() + " / " + rt.getTargetCode());
            ((TextView) res.findViewById(R.id.bottomtext)).setText("");
        return res;
    }

 //   private View getEmptyView(){
 //       View res = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
 //       return res;
  //  }

}
